$(function () {
    let goodsId = getQueryString("goodsId");
    let url = '../product/list';
    vm.goodsId = goodsId;
    if (goodsId) {
        url += '?goodsId=' + goodsId;
    }
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '规格分组', name: 'firstname', index: 'firstname', width: 120},
           /* {label: '商品', name: 'goodsName', index: 'goods_id', width: 120},*/
            {
                label: '商品规格',
                name: 'specificationValue',
                index: 'goods_specification_ids',
                width: 100,
                formatter: function (value, options, row) {
                    return value.replace(row.goodsName + " ", '');
                }
            },
            {label: '商品序列号', name: 'goodsSn', index: 'goods_sn', width: 80},
            {label: '商品库存', name: 'goodsNumber', index: 'goods_number', width: 80},
            {label: '零售价格(元)', name: 'retailPrice', index: 'retail_price',editable: true, width: 80},
            {label: '市场价格(元)', name: 'marketPrice', index: 'market_price',editable: true, width: 80}],
            groupingView : {
                groupField : ['firstname']
            }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        product: {},
        retailPrice: null,
        marketPrice: null,
        disabled1:false,
        disabled2:false,
        disabled3:false,
        disabled4:false,
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            goodsName: ''
        },
        goodss: [],
        attribute: [],
        goodsId:null,
        color: [], guige: [], weight: [],size: [],bak: [],bak0: [],bak1: [],bak2: [],bak3: [],bak4: [],bak5: [],bak6: [],bak7: [],bak8: [],bak9: [],bak10: [],
        colors: [],
        guiges: [],
        weights: [],
        sizes: [],
        baks: [],
        bak0s: [],bak1s: [],bak2s: [],bak3s: [],bak4s: [],bak5s: [],bak6s: [],bak7s: [],bak8s: [],bak9s: [],bak10s: [],
        type: ''
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.product = {};
            vm.getGoodss();
            vm.getGoods();
            vm.type = 'add';
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.type = 'update';

            vm.getInfo(id)
        },
        setprice: function (event) {
            let ids = getSelectedRows("#jqGrid");
            confirm('确定批量修改价格？', function () {
                var bsgs = [];
                for (var i = 0; i < ids.length; i++) {
                    var rowData = $("#jqGrid").jqGrid('getRowData',ids[i]);
                    var mproduct = {};
                    mproduct.id = ids[i];
                    if(vm.retailPrice>0)
                        mproduct.retailPrice = vm.retailPrice;
                    if(vm.marketPrice>0)
                        mproduct.marketPrice = vm.marketPrice;
                    Ajax.request({
                        url: "../product/update",
                        async: false,
                        params: JSON.stringify(mproduct),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function (r) {

                        }
                    });
                }
                alert('操作成功', function (index) {
                    vm.reload();
                });
            });
        },
        changeGoods: function (opt) {
            let goodsId = opt.value;
            Ajax.request({
                url: "../goods/info/" + goodsId,
                async: true,
                successCallback: function (r) {
                    if (vm.type == 'add') {
                        vm.product.goodsSn = r.goods.goodsSn;
                        vm.product.goodsNumber = r.goods.goodsNumber;
                        vm.product.retailPrice = r.goods.retailPrice;
                        vm.product.marketPrice = r.goods.marketPrice;
                    }
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=1",
                        async: true,
                        successCallback: function (r) {
                            vm.colors = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=2",
                        async: true,
                        successCallback: function (r) {
                            vm.guiges = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=4",
                        async: true,
                        successCallback: function (r) {
                            vm.weights = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=5",
                        async: true,
                        successCallback: function (r) {
                            vm.sizes = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=6",
                        async: true,
                        successCallback: function (r) {
                            vm.baks = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=7",
                        async: true,
                        successCallback: function (r) {
                            vm.bak0s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=8",
                        async: true,
                        successCallback: function (r) {
                            vm.bak1s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=9",
                        async: true,
                        successCallback: function (r) {
                            vm.bak2s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=10",
                        async: true,
                        successCallback: function (r) {
                            vm.bak3s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=11",
                        async: true,
                        successCallback: function (r) {
                            vm.bak4s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=12",
                        async: true,
                        successCallback: function (r) {
                            vm.bak5s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=13",
                        async: true,
                        successCallback: function (r) {
                            vm.bak6s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=14",
                        async: true,
                        successCallback: function (r) {
                            vm.bak7s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=15",
                        async: true,
                        successCallback: function (r) {
                            vm.bak8s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=16",
                        async: true,
                        successCallback: function (r) {
                            vm.bak9s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=17",
                        async: true,
                        successCallback: function (r) {
                            vm.bak10s = r.list;
                        }
                    });
                }
            });
        },
        getGoods: function () {
            Ajax.request({
                url: "../goods/info/" + vm.goodsId,
                async: true,
                successCallback: function (r) {
                    if (vm.type == 'add') {
                        vm.product.goodsSn = r.goods.goodsSn;
                        vm.product.goodsNumber = r.goods.goodsNumber;
                        vm.product.retailPrice = r.goods.retailPrice;
                        vm.product.marketPrice = r.goods.marketPrice;
                    }
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=1",
                        async: true,
                        successCallback: function (r) {
                            vm.colors = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=2",
                        async: true,
                        successCallback: function (r) {
                            vm.guiges = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=4",
                        async: true,
                        successCallback: function (r) {
                            vm.weights = r.list;
                        }
                    });

                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=5",
                        async: true,
                        successCallback: function (r) {
                            vm.sizes = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=6",
                        async: true,
                        successCallback: function (r) {
                            vm.baks = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=7",
                        async: true,
                        successCallback: function (r) {
                            vm.bak0s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=8",
                        async: true,
                        successCallback: function (r) {
                            vm.bak1s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=9",
                        async: true,
                        successCallback: function (r) {
                            vm.bak2s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=10",
                        async: true,
                        successCallback: function (r) {
                            vm.bak3s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=11",
                        async: true,
                        successCallback: function (r) {
                            vm.bak4s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=12",
                        async: true,
                        successCallback: function (r) {
                            vm.bak5s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=13",
                        async: true,
                        successCallback: function (r) {
                            vm.bak6s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=14",
                        async: true,
                        successCallback: function (r) {
                            vm.bak7s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=15",
                        async: true,
                        successCallback: function (r) {
                            vm.bak8s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=16",
                        async: true,
                        successCallback: function (r) {
                            vm.bak9s = r.list;
                        }
                    });
                    Ajax.request({
                        url: "../goodsspecification/queryAll?goodsId=0&specificationId=17",
                        async: true,
                        successCallback: function (r) {
                            vm.bak10s = r.list;
                        }
                    });
                }
            });
        },
        saveOrUpdate: function (event) {
            let url = vm.product.id == null ? "../product/save" : "../product/update";
            if(vm.product.id ==null)
            {
                vm.product.goodsSpecificationIds = "";
                if(vm.color!=null&&vm.color!="")
                    vm.product.goodsSpecificationIds += vm.color + '_' ;
                if(vm.guige!=null&&vm.guige!="")
                    vm.product.goodsSpecificationIds += vm.guige + '_' ;
                if(vm.weight!=null&&vm.weight!="")
                    vm.product.goodsSpecificationIds += vm.weight+ '_' ;
                if(vm.size!=null&&vm.size!="")
                    vm.product.goodsSpecificationIds += vm.size + '_' ;
                if(vm.bak!=null&&vm.bak!="")
                    vm.product.goodsSpecificationIds += vm.bak + '_' ;
                if(vm.bak1!=null&&vm.bak1!="")
                    vm.product.goodsSpecificationIds += vm.bak1 + '_' ;
                if(vm.bak0!=null&&vm.bak0!="")
                    vm.product.goodsSpecificationIds += vm.bak0 + '_' ;
                if(vm.bak2!=null&&vm.bak2!="")
                    vm.product.goodsSpecificationIds += vm.bak2 + '_' ;
                if(vm.bak3!=null&&vm.bak3!="")
                    vm.product.goodsSpecificationIds += vm.bak3 + '_' ;
                if(vm.bak4!=null&&vm.bak4!="")
                    vm.product.goodsSpecificationIds += vm.bak4 + '_' ;
                if(vm.bak5!=null&&vm.bak5!="")
                    vm.product.goodsSpecificationIds += vm.bak5 + '_' ;
                if(vm.bak6!=null&&vm.bak6!="")
                    vm.product.goodsSpecificationIds += vm.bak6 + '_' ;
                if(vm.bak7!=null&&vm.bak7!="")
                    vm.product.goodsSpecificationIds += vm.bak7 + '_' ;
                if(vm.bak8!=null&&vm.bak8!="")
                    vm.product.goodsSpecificationIds += vm.bak8 + '_' ;
                if(vm.bak9!=null&&vm.bak9!="")
                    vm.product.goodsSpecificationIds += vm.bak9 + '_' ;
                if(vm.bak10!=null&&vm.bak10!="")
                    vm.product.goodsSpecificationIds += vm.bak10 + '_' ;
                /*            if(vm.product.goodsSpecificationIds.length>0)
                                vm.product.goodsSpecificationIds*/
                vm.product.goodsId = vm.goodsId;
            }
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.product),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });


        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../product/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });


            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../product/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.product = r.product;
                    vm.getGoodss();
                    vm.getGoods();
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'goodsName': vm.q.goodsName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getGoodss: function () {
            Ajax.request({
                url: "../goods/queryAll/",
                async: true,
                successCallback: function (r) {
                    vm.goodss = r.list;
                }
            });
        }
    }
});